let myUrl = require("../utils/url.js")

let requests = require("../utils/requests.js")

/**
 * 获取轮播图
 */
export function getMaterialBanner(){
  return requests.fatch({
    load: 0,
    url: myUrl.mainUrl + "material_banner/"
  })
}

/**
 * 获取分类
 */
export function getMaterialCategory(){
  return requests.fatch({
    load: 0,
    url: myUrl.mainUrl + "material_category"
  })
}

/**
 * 获取最新资料
 */
export function getNewMaterial(next){
  if(next == null){
    return requests.fatch({
      load: 0,
      url: myUrl.mainUrl + "material?is_new=true",
    })
  }else{
    return requests.fatch({
      load: 0,
      url: next
    })
  }
}

/**
 * 获取指定分类的资料
 */
export function getCategoryMaterial(category_id){
  return requests.fatch({
    laod: 0,
    url: myUrl.mainUrl + "material?category=" + category_id
  })
}

/**获取资料详情 */
export function getMaterialDetail(material_id, next){
  if(next == null){
    return requests.fatch({
      load: 0,
      url: myUrl.mainUrl + "material/" + material_id + "/"
    })
  }else{
    return requests.fatch({
      load: 0,
      url: next
    })
  }
}

/**
 * 用户购买的所有资料
 */
export function getUserMaterial(){
  return requests.fatch({
    load: 0,
    url: myUrl.mainUrl + "users_material/",
    header: { "Authorization": "JWT " + wx.getStorageSync('token') }
  })
}


/**
 * 开始支付
 * @param {*} singer_mobile 
 * @param {*} course_id 
 */
export function singlePay(singer_mobile, material_id){
  return requests.fatch({
    url: myUrl.mainUrl + "material_order/",
    load: 1,
    data: {
      singer_mobile: singer_mobile,
      material_id: material_id
    },
    header: { "Authorization": "JWT " + wx.getStorageSync('token') },
    method: "POST"
  })
}